package com.deyuanyun.pic.settings.util;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import com.deyuanyun.pic.common.util.converter.BeanConverter;
import org.apache.commons.io.IOUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;

import com.deyuanyun.pic.common.spring.SpringAppliction;
import com.deyuanyun.pic.common.util.StringUtils;
import com.deyuanyun.pic.common.util.poi.ExcelUtil;
import com.deyuanyun.pic.common.util.web.RequestResponseContext;
import com.deyuanyun.pic.common.util.web.WebUtil;
import com.deyuanyun.pic.settings.controller.dto.org.CompanyLogoVO;
import com.deyuanyun.pic.settings.service.org.CompanyService;
import com.deyuanyun.pic.settings.support.interceptor.RequestAndResponseInterceptor;

import javax.servlet.http.HttpServletResponse;

public class DownExcelUtil {

    /**
     * 填充Excel的头部，为了适应统一头部为logo、标题。
     * 
     * @param workbook
     * @param fileName
     * @return
     */
    public static Workbook fillWorkbookHead(Workbook workbook){
        
        CompanyService companyService = SpringAppliction.getBean(CompanyService.class);
        
        CompanyLogoVO companyLogo = companyService.getUserCompanyLogo();
        byte[] logo_bytes = null;
        String companyName = "德源云管道";
        if(companyLogo != null){
            String logoPath = companyLogo.getLogoPath();
            
            if(StringUtils.isNotEmpty(logoPath)){
                FileInputStream fis = null;
                try{
                    fis = new FileInputStream(logoPath);
                    logo_bytes = IOUtils.toByteArray(fis);
                } catch (Exception e) {
                    e.printStackTrace();
                } finally{
                    try {
                        fis.close();
                    } catch (Exception e2) {
                        // TODO: handle exception
                    }
                }
            }
            companyName = companyLogo.getName();
        }
        
        String fileTitle = RequestResponseContext.getRequest().getParameter("fileName");
        if(StringUtils.isEmpty(fileTitle)){
            fileTitle = "-";
        }
        //String hz = workbook instanceof HSSFWorkbook? ".xls" : ".xlsx";
        //WebUtil.setDownHeader(fileName + hz);
        workbook = ExcelUtil.fillWorkbookHead(workbook, logo_bytes, companyName, fileTitle);
        return workbook;
    }


    /**
     * excel模板导出
     *
     * @param filePath 导出文件路径名称
     * @param tempPath excel模板路径
     * @param beginRow 开始写入行数
     * @param data list<T>数据
     * @param columnKeys 写入文件顺序（map的key顺序）
     * @throws IOException
     */
    public static <T>void export(String filePath, String tempPath, Integer beginRow, List<T> data, String[] columnKeys) throws IOException {
        List<Map> mapList = BeanConverter.toListMap(data);
        Workbook workbook = ExcelUtil.initWorkbook(tempPath, columnKeys, mapList, beginRow);
        DownExcelUtil.fillWorkbookHead(workbook);
        WebUtil.setDownHeader(filePath);
        HttpServletResponse response = RequestResponseContext.getResponse();
        workbook.write(response.getOutputStream());
    }

    /**
     * excel模板多个Sheet页签导出
     *
     * @param filePath
     * @param tempPath excel模板路径
     * @param sheetList
     * @throws IOException
     */
    public static void export(String filePath, String tempPath, List<ExcelUtil.SheetParam> sheetList) throws IOException {
        Workbook workbook = ExcelUtil.initWorkbook(tempPath, sheetList);
        DownExcelUtil.fillWorkbookHead(workbook);
        WebUtil.setDownHeader(filePath);
        HttpServletResponse response = RequestResponseContext.getResponse();
        workbook.write(response.getOutputStream());
    }
}
